﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;

namespace SMS.Helper
{
    public class ViewModelLocator
    {

        static ViewModelLocator()
        {
            //if (viewModels == null)
            //    RestoreData();
        }

        static List<Entities.BaseClasses.BaseNotify> viewModels = new List<Entities.BaseClasses.BaseNotify>();

        public static T GetViewModel<T>() where T : Entities.BaseClasses.BaseNotify
        {
            if (viewModels.FirstOrDefault(k => k is T) == null)
                viewModels.Add(Activator.CreateInstance<T>());
            return (T)viewModels.FirstOrDefault(k => k is T);
        }

        public static void StoreData()
        {
            WriteUserInfo(viewModels);
        }

        private static void RestoreData()
        {
            viewModels = GetUserInfoFromStorage<List<Entities.BaseClasses.BaseNotify>>();
        }

        private static T GetUserInfoFromStorage<T>()
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForSite())
            {
                if (store.FileExists(fileName))
                {

                    IsolatedStorageFileStream stream = store.OpenFile(fileName, System.IO.FileMode.Open);
                    List<Type> knownTypes = viewModels.Select(k => k.GetType()).ToList();
                    var serializer = new DataContractSerializer(typeof(T), knownTypes);
                    var userInfo = serializer.ReadObject(stream);
                    stream.Close();
                    return (T)userInfo;
                }
                return Activator.CreateInstance<T>();
                //MainPage mainpage = new MainPage(obj);
                //grid.Children.Clear();
                //grid.Children.Add(mainpage);
            }
        }
        static string fileName = "userStoredData.dat";
        private static bool WriteUserInfo(object objectToWrite)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForSite())
            {
                if (store.FileExists(fileName))
                    store.DeleteFile(fileName);

                if (!store.FileExists(fileName))
                {
                    IsolatedStorageFileStream stream = store.CreateFile(fileName);
                    List<Type> knownTypes = viewModels.Select(k => k.GetType()).ToList();
                    var serializer = new DataContractSerializer(objectToWrite.GetType(), knownTypes);
                    serializer.WriteObject(stream, objectToWrite);
                    stream.Close();
                }

                //MainPage mainpage = new MainPage(obj);
                //grid.Children.Clear();
                //grid.Children.Add(mainpage);
            }
            return true;
        }

    }
}
